package while_lang;

import java.util.*;
import java.io.*;

import java_cup.runtime.Symbol;
import while_lang.ast.*;
import while_lang.ast.metrics.HalsteadComplexityMeasures;
import while_lang.parser.ParserCup;

public class Test {
	static Random random = new Random();

	public static void main(String[] args) throws Exception {
		StringBuilder code = new StringBuilder();
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		for (String line = in.readLine(); line != null; line = in.readLine()) {
			if (line.equals(":exit")) {
				System.out.println("Exiting.");
				break;
			} else if (line.equals(":random")) {
				String randomCode = Sequence.generate(Test.random, 2, 3).unparse();
				code.append(randomCode);
				System.out.println(randomCode);
			} else if (line.length() > 0) {
				code.append(line +'\n');
			} else if (code.length() > 0) {
				try {
					System.out.println(new HalsteadComplexityMeasures(
							ParserCup.parseString(code.toString())
					));				
				} catch (Exception error) {
					System.err.println(error);
				}
				code = new StringBuilder();
			}	
		}
	}
}
